package site.teamo.biu.net.common.web.configuration;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import site.teamo.biu.net.common.exception.BiuNetBusinessException;
import site.teamo.biu.tool.core.model.BiuJSONResult;
import site.teamo.biu.tool.core.util.BiuJSONResultUtils;
import site.teamo.biu.tool.spring.exception.BiuToolValidationException;

import java.util.Map;

import static site.teamo.biu.net.common.web.constant.ResponseCode.PARAMETER_ERROR;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BiuToolValidationException.class)
    public BiuJSONResult<Map<String,String>> handleException(BiuToolValidationException e) {
        return BiuJSONResultUtils.error(
                PARAMETER_ERROR,
                String.join(",", e.getValidationResult().values()),
                e.getValidationResult());
    }

    @ExceptionHandler(BiuNetBusinessException.class)
    public BiuJSONResult<Object> handleException(BiuNetBusinessException e) {
        return BiuJSONResultUtils.error(e.getCode(), e.getMessage());
    }

}
